package com.lcb.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.blog.base.BaseApiService;
import com.lcb.blog.base.BaseResponse;
import com.lcb.blog.entity.User;
import com.lcb.blog.mapper.UserMapper;
import com.lcb.blog.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;

/****
 * @Author:lcb
 * @Description:User业务层接口实现类
 * @Date: 2020-12-29 14:38:32
 *****/
@Api(tags = "UserServiceImpl")
@RestController
//@RequestMapping("/blog")
//@CrossOrigin
public class UserServiceImpl extends BaseApiService implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * User条件+分页查询
     * @param user 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @ApiOperation(value = "User条件查询",notes = "条件查询User方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse findUserList(@RequestBody User user, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size){
        //分页
        PageHelper.startPage(page,size);

        PageInfo<User> pagelist =new PageInfo<User>(userMapper.select(user));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     * @param ids
     */
    @ApiOperation(value = "User根据ID删除",notes = "根据ID删除User方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse deleteUserById (@PathVariable(value = "ids") String ids){

        String[] split = ids.split(",");
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        userMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改User
     * @param user
     */
    @ApiOperation(value = "User根据ID修改",notes = "根据ID修改User方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse updateUser(@RequestBody User user){
        userMapper.updateByPrimaryKeySelective(user);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加User
     * @param user
     */
    @ApiOperation(value = "User添加",notes = "添加User方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse addUser(@RequestBody User user){
        userMapper.insertSelective(user);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询User
     * @param id
     * @return
     */
    @ApiOperation(value = "User根据ID查询",notes = "根据ID查询User方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse findUserById(@PathVariable(value = "id") Integer id){
        return setResultSuccess( userMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询User全部数据
     * @return
     */
    @ApiOperation(value = "查询所有User",notes = "查询User所有方法详情",tags = {"UserServiceImpl"})
    @Override
    public BaseResponse findUserAll() {
        return setResultSuccess(userMapper.selectAll());
    }
}
